/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
function Bird(data) {
    this.image = new Image();
    this.image.src = data.imgSrc;
    this.frameWidth = data.frameWidth;
    this.frameHeight = data.frameHeight;
    this.numFrame = data.numFrame;
    this.cx = 300;
    this.maxLoop = 7;
    this.countLoop = 0;
}
Bird.prototype = {
    gameReady: function() {
        this.cy = 300;
        this.gravity = 0;
        this.rotation = 0;
        this.stepJump = 0;
        this.velocity = 0;
        this.frameCurrent = 0;
        this.angle = 0;
    },
    gamePlay: function() {
        this.gravity = 0.25;
        this.stepJump = 6;
        this.rotation = 0.3;
    },
    jump: function() {
        this.velocity = -this.stepJump;
    },
    update: function() {
        this.countLoop = (this.countLoop + 1) % this.maxLoop;
        if (this.countLoop == 0)
            this.frameCurrent = (this.frameCurrent + 1) % this.numFrame;
        this.velocity += this.gravity;
        this.cy += this.velocity;
        if (this.velocity >= this.stepJump) {
            this.angle = Math.min(Math.PI / 2, this.rotation + this.angle);
        } else {
            this.angle = -0.1;
        }
    },
    draw: function(context) {
        context.save();
        context.translate(this.cx, this.cy);
        context.rotate(this.angle);
        context.drawImage(this.image, this.frameCurrent * this.frameWidth, 0, this.frameWidth, this.frameHeight,
                -this.frameWidth / 2, -this.frameHeight / 2, this.frameWidth, this.frameHeight);
        context.restore();
    }
}


